import 'package:flutter/cupertino.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/pages/profile/models/model_security_question.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:get/get.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/log/utils_log.dart';

class ProfileEditTrcLogic extends BaseController {
  TextEditingController answerController = TextEditingController();
  RxBool isFormValid = false.obs;
  RxString fundPassword = ''.obs;

  TextEditingController newAddressController = TextEditingController();
  Rx<ModelSecurityQuestion> securityQuestion = Rx(ModelSecurityQuestion());

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    getUserSecurityQuestion();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
    answerController.dispose();
    newAddressController.dispose();
  }

  /// 表单验证
  void validateForm() {
    final isValid = newAddressController.text.length > 10 &&
        answerController.text.trim().isNotEmpty &&
        fundPassword.value.length == 6;
    isFormValid.value = isValid;
  }

  getUserSecurityQuestion() async {
    HttpResponse response = await HttpManager().get(API.getUserSecurityQuestion,
        fromJson: ModelSecurityQuestion.fromJson);
    if (response.isSuccess) {
      securityQuestion.value = response.data;
    }
  }

  postUserChangeTrc20Address() async {
    HttpResponse response =
        await HttpManager().post(API.postUserChangeTrc20Address,
            // fromJson: .fromJson,
            params: {
              "address": newAddressController.text.trim(),
              "answer": answerController.text.trim(),
              "fund_password": fundPassword.value
            },
            uiType: HttpUIType.loadingAndToast);
    if (response.isSuccess) {
      LoadingUtils.showToast(LocaleKeys.setSuccess.tr);
      AppUserManager().refreshUserInfo();
      Get.back();
    }
  }
}
